package com.finger.forest.utils;


import android.os.IInterface;
import android.util.Log;

import com.finger.forest.base.BaseApi;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static java.lang.String.valueOf;

/**
 * @author jzj
 * @date 2020/10/5
 * @desc
 */
public class RequestUtil {

    private static final String TAG = "RequestUtil";

    /**
     * http post请求
     * @param url
     * @param map
     */
    public static String httpPost(String url, Map<String, Object> map) {
        //实例
        OkHttpClient okHttpClient = new OkHttpClient();

        //创建post请求数据表单
        FormBody.Builder builder = new FormBody.Builder();
        for (Map.Entry entry : map.entrySet()) {
            builder.add(valueOf(entry.getKey()), valueOf(entry.getValue()));
        }

        //创建请求
        final Request request = new Request.Builder()
                .url(BaseApi.BASE_URL + url)
                .post(builder.build())//添加post请求
                .build();
        try {
            //发送请求得到响应
            final Response response = okHttpClient.newCall(request).execute();
            String result = response.body().string();
            System.out.println(result);
            return result;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * get请求
     * @param url
     * @param callback
     */
    public static void httpGet(final String url, final Callback callback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder().url(url).addHeader("token","").build(); //添加头部信息
                okHttpClient.newCall(request).enqueue(callback);
            }
        }).start();
    }

    public static void main(String[] args) {
        /*Map<String,Object> mapParams = new HashMap<>();
        mapParams.put("username","jzj");
        mapParams.put("password","123456");
        mapParams.put("isonline","0");
        httpPost(BaseApi.USER_LOGIN,mapParams);*/

        /*String vedioLoginUrl = "http://192.168.1.240/prod-api/surveillance/hkSdk/login?ip=192.168.1.63&port=8000&userName=admin&password=ahly12345";
        //海康视频监控登录
        RequestUtil.httpGet(vedioLoginUrl, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(TAG+"海康视频登录失败");
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(TAG+"海康视频登录成功");
                String result = response.body().string();//得到返回的数据
                System.out.println(result);
            }
        });*/

        String vedioLoginUrl = "https://blog.csdn.net/ApplicationDev/article/details/116785060";
        //海康视频监控登录
        RequestUtil.httpGet(vedioLoginUrl, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println(TAG+"访问失败");
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(TAG+"访问成功");
                String result = response.body().string();//得到返回的数据
                System.out.println(result);
            }
        });
    }

}
